Skip to main content

ReduxModelGenerator

The way to create a model is by using the ReduxModelGenerator class. Typically you would implement your own ModelGenerator class in your project, that extends ReduxModelGenerator. There you can pass global configs that apply to all generated models.

Here is an example how to extend the ReduxModelGenerator class. Notice that the defaultConfig of our entire app is merged with the config passed for a single model generator class, therefore allowing you to overwrite any global configuration.

const defaultConfig: ModelGeneratorOptions = {
entities: {
api: {
createItem: (type, args) => api.post(`/${type}`, args),
fetchList: (type, args) => api.get(`/${type}`, args),
...
}
},
valueLists: {
api: {
fetch: (type) => api.get(`lists/${type}`)
}
}
};

export class ModelGenerator<DataType, CustomActions = Record<string, never>> extends ReduxModelGenerator<DataType, CustomActions> {
constructor(type: string, config: GeneratorConfig = {}) {
super(type, merge({}, defaultConfig, config));
}
}

Models

There are 3 different types of models that ReduxModelGenerator is able to generate.

  • CustomModel
  • EntityModel
  • ValueListModel
// Creates and returns a custom model
generator.createModel(options);

// Creates and returns an entity model
generator.createEntityModel(options);

// Creates and returns a value list model
generator.createValueListModel(options);

CustomModel

The CustomModel does not come with any predefined actions like createItem, updateItem etc. This is most useful for models that primarily deal with local data, that dont require interactions with the API.

EntityModel

An EntityModel does come with the basic CRUD actions. This is the model that you will use most of the time, as a lot/if not all APIs at Rexlabs are build with the same CRUD interface.

ValueListModel

A ValueListModel does not come with any actions since a value list is not mutatable by the client. Data loaded for a ValueListModel is also not garbage collected and is cached indefinitely, since it is not likely that it will change during the lifetime of the user session.